package com.iking.housekeeping.service.servicestaff;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.iking.housekeeping.entity.dept.SysDept;
import com.iking.housekeeping.httpbean.elderlyreq.HttpElderlyReq;
import com.iking.housekeeping.httpbean.elderlyres.HttpElderlyRes;
import com.iking.housekeeping.service.dept.DeptService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iking.housekeeping.dao.servicerecord.ServiceRecordMapper;
import com.iking.housekeeping.dao.servicestaff.ServiceStaffMapper;
import com.iking.housekeeping.entity.servicestaff.ServiceStaff;
import com.iking.housekeeping.httpbean.elderlyres.ElderlyRes;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.DetailsServiceStaff;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.HttpGiveElderly;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.SelectServiceRes;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.StaffBySelect;
import com.iking.housekeeping.util.UUIDUtil;
import com.iking.housekeeping.util.token.WebTokenUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.region.Region;

/**
 * 服务人员
 * 
 * @author: gf
 * @date: 2018年4月18日 下午5:48:17
 */
@Service
public class ServiceStaffService {
	
	/**
	 * 服务人员操作
	 */
	@Autowired
	private ServiceStaffMapper serviceStaffMapper; // 服务人员操作

	@Autowired
	private ServiceRecordMapper serviceRecordMapper;


	@Autowired
	private DeptService deptService;

	/**
	 * 添加服务人员
	 * 
	 * @param req
	 * @return ReturnCode
	 * @author gf
	 */
	public int insert(ServiceStaff req) {
		return serviceStaffMapper.insertSelective(req);
	}

	/**
	 * 删除服务人员
	 * 
	 * @param fId
	 * @return int
	 * @author gf
	 */
	public int delete(String fId) {

		return serviceStaffMapper.updateByFidPrimaryKey(fId);
	}

	/**
	 * 根据id的查询服务人员
	 * 
	 * @param fId
	 * @return int
	 * @author gf
	 */
	public ServiceStaff selectByPrimaryKey(String fId) {
		return serviceStaffMapper.selectByPrimaryKey(fId);
	}

	/**
	 * 更新服务人员信息
	 * 
	 * @param req
	 * @return int
	 * @author gf
	 */
	public int update(ServiceStaff req) {
		return serviceStaffMapper.updateByPrimaryKeySelective(req);
	}

	/**
	 * 批量删除服务人员
	 * 
	 * @param list
	 * @return int
	 * @author gf
	 */
	public int deleteBatch(List<String> list) {
		return serviceStaffMapper.deleteBatch(list);
	}
	/**
	 * 查询所有服务人员基本信息
	 * 
	 * @return List<ServiceStaff> 服务人员基本信息集合
	 * @author gf
	 */
	// public Page<ServiceStaff> selectAll(PageParam pageParam){
	// PageHelper.startPage(pageParam.getPageCurrent(), pageParam.getPageSize());
	// return serviceStaffMapper.selectAll();
	// }

	/**
	 * 通过单个属性查询服务人员
	 * 
	 * @param req
	 * @return ServiceStaff
	 * @author gf
	 */
	public ServiceStaff selectRepeat(ServiceStaff serviceStaff) {
		return serviceStaffMapper.selectRepeat(serviceStaff);
	}

	/**
	 * 分页显示服务人员信息
	 * 
	 * @param staffBySelect
	 * @return Page<SelectServiceRes>
	 * @author gf
	 */
	public Page<SelectServiceRes> selectAll(StaffBySelect staffBySelect, javax.servlet.http.HttpServletRequest request) {
		//获取当前登录人id
	    String userId = WebTokenUtil.getUserIdByRequest(request);
//		String userId=request.getHeader("accessToken").toString();
		if(staffBySelect.getfStreetCommunity()==null || "".equals(staffBySelect.getfStreetCommunity())) {
			List<SysDept> menuList = deptService.selectAdminById(userId);
			List list = deptService.selectAdminById(userId);
			//判断当前登录人是否为区域管理员
			if ("".equals(menuList.get(0).getfDeptPCode()) || menuList.get(0).getfDeptPCode() == null) {
				//查看所有区域数据
				staffBySelect.setfStreetCommunity(menuList.get(0).getfId());
			} else {
				if (list.contains(menuList.get(0).getfDeptPCode()) || menuList.get(0).getfAdminUser() != "") {
					//查看二级服务数据
					staffBySelect.setfStreetCommunity(menuList.get(0).getfId());
				} else {
					//查看自己服务数据
					staffBySelect.setFstaffId(userId);
				}
			}
		}
		PageHelper.startPage(staffBySelect.getPageCurrent(), staffBySelect.getPageSize());
		Page<SelectServiceRes> selectServiceStaff = serviceStaffMapper.selectServiceStaff(staffBySelect);
		return selectServiceStaff;
	}

	/**
	 * 通过id查询详情
	 * 
	 * @param fId
	 * @return SelectServiceRes
	 * @author gf
	 */
	public DetailsServiceStaff selectById(String fId) {
		return serviceStaffMapper.selectById(fId);
	}

	
	/**
	 * 	展示分配人员页面
	 *  @return List<HttpGiveElderly>
	 *  @author gf
	 */
	public List<HttpGiveElderly> giveServiceStaff(){
		return serviceStaffMapper.giveServiceStaff();
	}

	/**
	 * 
	 * @Description:查询服务人员的当前服务
	 * @author:CGZ
	 * @param request
	 * @return
	 */
	public ElderlyRes selectCurrentService(HttpServletRequest request) {
		String staffId = WebTokenUtil.getUserIdByRequest(request);
		return serviceRecordMapper.selectCurrentService(staffId);
	}

	/**
	 * 
	 * @Description:上传图片到云端
	 * @author:CGZ
	 */
	public String uploadImage(MultipartFile files) {
		int count = 0;
		InputStream inputStream = null;
			try {
					count = files.getInputStream().available();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				inputStream = files.getInputStream();
			} catch (IOException e) {
				e.printStackTrace();
			}
		// 1 初始化用户身份信息(secretId, secretKey)
		COSCredentials cred = new BasicCOSCredentials("AKIDYMilXotVIxjN6wqRGhiiZidwHzbOxYql",
				"jc6hGP55Z8tcNSiqwn0UxMrCk2ZbyyHv");
		// 2 设置bucket的区域, COS地域的简称请参照
		// https://cloud.tencent.com/document/product/436/6224
		ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
		// 3 生成cos客户端
		COSClient cosclient = new COSClient(cred, clientConfig);
		// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
		String bucketName = "zhu-you-jia-zheng-1251253256";
		// 文件名称
		
		String key = UUIDUtil.getUUID() + ".png";
		// 方法2 从输入流上传(需提前告知输入流的长度, 否则可能导致 oom)
		ObjectMetadata objectMetadata = new ObjectMetadata();
		// 设置输入流长度为 500
		objectMetadata.setContentLength(count);
		// 设置 Content type, 默认是 application/octet-stream
		objectMetadata.setContentType("application/pdf");
		cosclient.putObject(bucketName, key, inputStream, objectMetadata);
		return "https://zhu-you-jia-zheng-1251253256.cos.ap-chengdu.myqcloud.com/"+key;
	}
	
	public List<String> selectStaffBySelectList(List<String> fId){
		return serviceStaffMapper.selectStaffBySelectList(fId);
	}
	
	/**
	 * 根据服务类型id查询服务人员信息
	 * @param fIds
	 * @return
	 */
	public List<ServiceStaff> selectStaff(List<String> fIds){
		return serviceStaffMapper.selectStaffByFId(fIds);
	}

//	public List<ServiceStaff> giveServiceStaffByIDAndType(StaffBySelect staffBySelect){
//		return serviceStaffMapper.giveServiceStaffByIDAndType(staffBySelect);
//			}

	/**
	 * 根据老人所在区域和服务类型获取该服务人员所分配的老人
	 * @param record
	 * @return  List<HttpGiveElderly>
	 */
	public List<HttpElderlyReq> selectElderlyInfo (HttpElderlyReq record){
		return serviceStaffMapper.selectElderlyInfo(record);
	}


}
